<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class tiny_controller extends wxapp_controller{
	function list_action(){//微简历列表
		$where="status='1'";
		$sdate=$_POST['sdate'];
		$edate=$_POST['edate'];
		$keyword=$this->stringfilter($_POST['keyword']);
		$page=$_POST['page'];
		$sex=(int)$_POST['sex'];
		$edu=(int)$_POST['edu'];
		$limit=$_POST['limit'];
		$order=$_POST['order'];
		$nodata=$_POST['nodata'];
		$limit=!$limit?10:$limit;
		if($sex){//类别ID
			$where.=" and `sex`='".$sex."'";
		}
		if($edu){//类别ID
			$where.=" and `edu`='".$edu."'";
		}
		if($sdate){//开始时间
			$where.=" and `time`>'".strtotime($sdate)."'";
		}
		if($edate){//结束时间
			$where.=" and `time`<'".strtotime($edate)."'";
		}
		if($keyword){//关键字
			$where.=" and (`username` like '%".$keyword."%' or `job` like '%".$keyword."%')";
		}
		if($nodata){//排除没有值的字段
			$nodataarr=explode(",",$nodata);
			foreach($nodataarr as $v){
				$where.=" and ".$v."<>''";
			}
		}
		if($order){//排序
			$where.=" order by ".$order;
		}else{
			$where.=" order by id desc";
		}
		if($page){//分页
			$pagenav=($page-1)*$limit;
			$where.=" limit $pagenav,$limit";
		}else{
			$where.=" limit $limit";
		}
		$rows=$this->obj->DB_select_all("resume_tiny",$where);
		
		if($rows && is_array($rows)){
			include PLUS_PATH."/user.cache.php";
			include(CONFIG_PATH."db.data.php");	
		    unset($arr_data['sex'][3]);	
		    $this->yunset("arr_data",$arr_data);
		    $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));//今天开始时间戳
		    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));//昨天开始时间戳
		    $week=strtotime(date("Y-m-d",strtotime("-1 week")));//一周内时间戳
			foreach($rows as $key=>$va){
				$list[$key]['id']		=$va['id'];
				$list[$key]['name']		=$va['username'];
				$list[$key]['sex']		=$va['sex'];
				$list[$key]['exp']		=$va['exp'];
				$list[$key]['job']		=$va['job'];
				$list[$key]['mobile']	=$va['mobile'];
				$list[$key]['production']=$va['production'];
				$list[$key]['sex_n']    =$arr_data['sex'][$va['sex']];
				$list[$key]['exp_n']    =$userclass_name[$va['exp']];
				$list[$key]['time']		=$va['time'];
				if($va['lastupdate']){
				    if ($va['lastupdate']>$week && $va['lastupdate']<$beginYesterday){
				        $list[$key]['lastupdate_n']="一周内";
				    }elseif($va['lastupdate']>$beginYesterday && $va['lastupdate']<$beginToday){
				        $list[$key]['lastupdate_n']="昨天";
				    }elseif($va['lastupdate']>$beginToday){
				        $list[$key]['lastupdate_n'] = date("H:i",$va['lastupdate']);
				    }else{
				        $list[$key]['lastupdate_n'] = date("Y-m-d",$va['lastupdate']);
				    }
				}
			}
			foreach($list as $k=>$v){
				if(is_array($v)){
					foreach($v as $key=>$val){
						$list[$k][$key]=isset($val)?$val:'';
					}
				}else{
					$list[$k]=isset($v)?$v:'';
				}
			}
			
			$data['list']=count($list)?$list:array();
			
			$data['error']=1;
		}else{
			$data['error']=2;
		}
		echo json_encode($data);die;
	}
	function show_action(){//微简历内容
		$id=(int)$_POST['id'];
		if(!$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("resume_tiny","`id`='".$id."'");
		$M=$this->MODEL('tiny');
	    $M->UpdateTinyresume(array("`hits`=`hits`+1"),array('id'=>$id));
		if(is_array($row)){
			include PLUS_PATH."/user.cache.php";
			$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));//今天开始时间戳
		    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));//昨天开始时间戳
		    $week=strtotime(date("Y-m-d",strtotime("-1 week")));//一周内时间戳
			$list=array();
			$list['id']		=$row['id'];
			$list['hits']		=$row['hits'];
			$list['name']	=$row['username'];
			$list['sex']	=$row['sex'];
			$list['exp']	=$row['exp'];
			$list['job']	=$row['job'];
			$list['mobile']	=$row['mobile'];
			$list['production']	=$row['production'];
			$list['exp_n']        =$userclass_name[$row['exp']];
			$list['time']	=$row['time'];
			if ($row['time']>$week && $row['time']<$beginYesterday){
				    $list['lastupdate_n']="一周内";
				}elseif($row['time']>$beginYesterday && $row['time']<$beginToday){
				    $list['lastupdate_n']="昨天";
				}elseif($row['time']>$beginToday){
				    $list['lastupdate_n'] = date("H:i",$row['time']);
				}else{
				    $list['lastupdate_n'] = date("Y-m-d",$row['time']);
				}
			$list['user_wjl_link']	=$this->config['user_wjl_link'];
			foreach($list as $k=>$v){
				if(is_array($v)){
					foreach($v as $key=>$val){
						$list[$k][$key]=isset($val)?$val:'';
					}
				}else{
					$list[$k]=isset($v)?$v:'';
				}
			}
			$data['user_wjl_link']=$this->config['user_wjl_link'];
			$data['list']=count($list)?$list:array();
			$data['error']=1;
		}else{
			$data['error']=2;
		}
		echo json_encode($data);die;
	}

	function add_action(){
	    $_POST=$this->post_trim($_POST);
		if($_POST['submit']){
			if(!$_POST['username'] || !$_POST['password'] || !$_POST['sex'] || !$_POST['exp'] || !$_POST['mobile'] || !$_POST['production'] || !$_POST['job']){
				$data['error']=3;
				$data['msg']='请完善信息！';
				echo json_encode($data);die;
			}
			$sql['username']=$this->stringfilter($_POST['username']);
			$sql['password']=md5($_POST['password']);
			$sql['job']=$this->stringfilter($_POST['job']);
			$sql['production']=$this->stringfilter($_POST['production']);
			$sql['sex']=intval($_POST['sex']);
			$sql['exp']=intval($_POST['exp']);
			$sql['mobile']=$_POST['mobile'];
			$sql['status']=$this->config['user_wjl'];
			$sql['lastupdate']=time();
			if($_POST['id']){
				$id=$this->obj->update_once("resume_tiny",$sql,array("id"=>(int)$_POST['id']));
				if($id){
					$data['id']=$_POST['id'];
					if($this->config['user_wjl']=="1"){
						$data['error']=1;
						$data['msg']='修改成功！';
					}else{
						$data['error']=1;
						$data['msg']='修改成功，等待审核！';
					}
				}else{
					$data['error']=2;
					$data['msg']='修改失败！';
				}
			}else{
			    $M=$this->MODEL('tiny');
			    $ip = fun_ip_get();
			    $s_time=strtotime(date('Y-m-d 00:00:00')); //今天开始时间
			    $m_tiny=$M->GetTinyresumeNum(array('login_ip'=>$ip,'`time`>\''.$s_time.'\''));
			    if($this->config['sy_tiny']>$m_tiny||$this->config['sy_tiny']<1){
			        $sql['time']=time();
				    $nid=$this->obj->insert_into("resume_tiny",$sql);
			    	if($nid){
    					$data['id']=$nid;
    					if($this->config['user_wjl']=="1"){
    						$data['error']=1;
    						$data['msg']='发布成功！';
    					}else{
    						$data['error']=1;
    						$data['msg']='发布成功，等待审核！';
    					}
    				}else{
    					$data['error']=2;
    					$data['msg']='提交失败！';
    				}
			    }else{
			        $data['error']=2;
			        $data['msg']="一天内只能发布".$this->config['sy_tiny']."次";
			    }
			}

		}else{
		    $M=$this->MODEL('tiny');
		    $ip = fun_ip_get();
		    $s_time=strtotime(date('Y-m-d 00:00:00')); //今天开始时间
		    $m_tiny=$M->GetTinyresumeNum(array('login_ip'=>$ip,'`time`>\''.$s_time.'\''));
		    if($this->config['sy_tiny']>$m_tiny||$this->config['sy_tiny']<1||$_POST['id']){
				include PLUS_PATH."/user.cache.php";
				include(CONFIG_PATH."db.data.php");	
				$row=$this->obj->DB_select_once("resume_tiny","`id`='".(int)$_POST['id']."'");
				$data['id']	=$row['id'];
				$data['username']	=$row['username'];
				$data['sex']=$row['sex'];
				$data['sex_n']	=$arr_data['sex'][$row['sex']];
				$data['exp']	=$row['exp'];
				$data['exp_n']=$userclass_name[$row['exp']];
				$data['job']=$row['job'];
				$data['mobile']	=$row['mobile'];
				$data['production']	=$row['production'];
		    }else{
		        $data['error']=2;
		        $data['msg']="一天内只能发布".$this->config['sy_tiny']."次";
		    }
		}
		echo json_encode($data);die;
	}
	function pass_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("resume_tiny","`id`='".$id."'","password");
		if(is_array($row)){
			if($row['password']==md5($_POST['password'])){
				$data['error']=1;
			}else{
				$data['error']=2;
			}
		}else{
			$data['error']=4;
		}
		echo json_encode($data);die;
	}
	function del_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("resume_tiny","`id`='".$id."'","password");
		if(is_array($row)){
			if($row['password']==md5($_POST['password'])){
				$id=$this->obj->DB_delete_all("resume_tiny","`id`='".$id."'");
				$data['error']=1;
			}else{
				$data['error']=2;
			}
		}else{
			$data['error']=4;
		}
		echo json_encode($data);die;
	}
	function editctime_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("resume_tiny","`id`='".$id."'","password");
		if(is_array($row)){
			if($row['password']==md5($_POST['password'])){
				$nid=$this->obj->DB_update_all("resume_tiny","`time`='".mktime()."'","`id`='".$id."'");
				if($nid){
					$data['error']=1;
				}else{
					$data['error']=5;
				}
			}else{
				$data['error']=2;
			}
		}else{
			$data['error']=4;
		}
		echo json_encode($data);die;
	}
	function isadd_action(){
	    $data['sy_tiny']=$this->config['sy_tiny'];
	    $M=$this->MODEL('tiny');
	    $ip = fun_ip_get();
	    $s_time=strtotime('today'); //今天开始时间
	    $m_tiny=$M->GetTinyresumeNum(array('login_ip'=>$ip,'`time`>\''.$s_time.'\''));
	    if($this->config['sy_tiny']>$m_tiny||$this->config['sy_tiny']<1){
	        $data['isadd']=true;
	    }else{
	        $data['isadd']=false;
	    }
	    echo json_encode($data);die;
	}
}
?>